package cn.xiaobai.captcha.handler;

import cn.hutool.extra.spring.SpringUtil;
import cn.xiaobai.captcha.config.CaptchaProperties;
import org.springframework.beans.factory.InitializingBean;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 类型处理器
 *
 * @Author yangdaji
 * @Date 2024/4/18
 * @PackageName cn.xiaobai.captcha.handler
 * @ClassName AbstractTypeHandler
 */
public abstract class AbstractTypeHandler implements InitializingBean {

    private static CaptchaProperties captchaProperties;

    public AbstractTypeHandler() {
        this.initConfig(null);
    }

    public AbstractTypeHandler(CaptchaProperties captchaProperties) {
        this.initConfig(captchaProperties);
    }

    /**
     * 初始化工厂, 在默认情况下从容器中获取
     */


    private void initConfig(CaptchaProperties captchaProperties) {
        if (Objects.isNull(AbstractTypeHandler.captchaProperties)) {
            AbstractTypeHandler.captchaProperties = Objects.isNull(captchaProperties) ? SpringUtil.getBean(CaptchaProperties.class) : captchaProperties;
        }
    }


    // 可以测试用

    /**
     * 获取过期时间
     *
     * @return
     */
    protected Integer getExpire() {
        return captchaProperties.getExpire();
    }

    /**
     * 获取时间单位
     *
     * @return
     */
    protected TimeUnit getTimeUnit() {
        return captchaProperties.getTimeUnit();
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
